
define(function(require, exports, module) {
    var $=require('zepto');
    api=require('api');
    cn=require('conment');
    pop=require('popup');
    var tel=/^1[3|4|5|7|8]\d{9}$/;
    var pswd=/^[0-9A-Za-z]+$/;
    module.exports = {
        p:{
            timeTxt:'发送验证码',
            time:60,
            istime:false,
        },

        init:function(option){
            var _this=this;
            $('#register-btn').on('click',function () {
                var t=$.trim($("#register-tel").val()),
                    p=$.trim($("#register-pswd").val()),
                    p2=$.trim($("#register-pswd2").val()),
                    code=$.trim($("#register-code").val());
                var next=_this.registerRex(t,p,p2,code);
                if(next!='true'){
                    pop.popup_err_msg({
                        id:'#popup-p-error',
                        text:next
                    })
                }else{
                    var url='';
                    if(!reg){
                        url=api.forget
                    }else{
                        url=api.register
                    }
                    cn.Ajax(url,{
                        username :t,
                        code:code,
                        password :p,
                        passwordConfirm :p2,
                    },function(d){
                        if(d.flag!=1){
                            pop.popup_err_msg({
                                id:'#popup-p-error',
                                text:d.message
                            })
                        }else{
                            $('#reg-form-step1').hide();
                            $('#reg-form-step3').show();
                            //window.location.href='./login.html'
                        }
                    },'post')
                }
            });
            $('#codeTxt').on('click',function () {
                var t=$.trim($("#register-tel").val());
                var m=$.trim($("#img-code").val());
                var txt=$('#codeTxt').text();
                var next=_this.isTel(t,m);
                if(next!='true'){
                    pop.popup_err_msg({
                        id:'#popup-p-error',
                        text:next
                    })
                }else{
                    if(_this.p.istime)return
                    _this.p.istime=true
                    _this.p.time=60;
                    cn.Ajax(api.getVerifyCode,{
                        "mobile" : t,
                        "vcode" : m
                    },function(d){
                        if(d.flag==1){
                            _this.getcodeTime(txt)
                        }else{
                            pop.popup_err_msg({
                                id:'#popup-p-error',
                                text:d.message
                            });
                            _this.p.istime=false
                        }

                    },'post')
                }
            });
            $('#codeImg').attr('src',api.getPicCode+'?ds='+Math.round(new Date().getTime()/1000))
            $('#codeImg').on('click',function () {
                $('#codeImg').attr('src',api.getPicCode+'?ds='+Math.round(new Date().getTime()/1000))
            });

        },
        getcodeTime:function (txt) {
            var _this=this
            if(!_this.p.istime) return;
            _this.p.time>0?$('#codeTxt').text(_this.p.time+'s'):$('#codeTxt').text('重新获取');
            --_this.p.time;
            _this.p.time<0?_this.p.istime=false:_this.p.istime=true
            setTimeout(function () {
                _this.getcodeTime()
            },1000);
        },
        loginRex:function (t,p) {
            return t==""?'请输入手机号码'
                :!tel.test(t)?'手机号码格式错误'
                    :(p==''||p.length<6)?'请输入6-18位数字，英文字母'
                        :!pswd.test(p)?'密码格式错误':'true'
        },
        registerRex:function (t,p,p2,c) {
            return t==""?'请输入手机号码'
                :!tel.test(t)?'手机号码格式错误'
                    :c==''?'请输入验证码'
                        :(p==''||p.length<6)?'请输入6-18位数字，英文字母'
                            :!pswd.test(p)?'密码格式错误'
                                :p!=p2?'密码输入不一致':'true'
        },
        isTel:function (t,c) {
            return t==""?'请输入手机号码'
                :!tel.test(t)?'手机号码格式错误'
                    :c==""?'输入图片验证码':'true'
        },



    }
});

